# -*- coding: utf-8 -*-
# Author     :wang_tuo
# Time       :2021/12/31 18:07

import ddddocr
import time


def dddd_ident(file, mode="b", ocr=None):
    if mode == "b":  # 传图片二进制
        ocr = ddddocr.DdddOcr(show_ad=False) if ocr is None else ocr
        result = ocr.classification(file)
        return result
    elif mode == "p":  # 传图片路径
        with open(file, "rb") as f:
            bdata = f.read()
        ocr = ddddocr.DdddOcr(show_ad=False) if ocr is None else ocr
        result = ocr.classification(bdata)
        return result


def getImgText1(bdata: bytes, ocr=None):
    begin = time.time()
    ocr = ddddocr.DdddOcr(show_ad=False) if ocr is None else ocr
    r = ocr.classification(bdata)
    print("识别结果", r)
    finish = time.time()
    print("cons time {} S".format(finish - begin))


def getImgText2(imgPath: str, ocr=None):
    begin = time.time()
    with open(imgPath, 'rb') as f:
        bdata = f.read()
    ocr = ddddocr.DdddOcr(show_ad=False) if ocr is None else ocr
    r = ocr.classification(bdata)
    print("识别结果", r)
    finish = time.time()
    print("cons time {} S".format(finish - begin))


if __name__ == '__main__':
    file = "../vcode/abcd.png"
    print(dddd_ident(file, mode="p"))
